/**
 * Copyright &copy; 2012-2014 <a href="http://www.iframework.cn">Mas</a> All rights reserved.
 */
package com.iframework.mas.modules.accident.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.iframework.mas.common.config.Global;
import com.iframework.mas.common.persistence.Page;
import com.iframework.mas.common.web.BaseController;
import com.iframework.mas.common.utils.StringUtils;
import com.iframework.mas.modules.accident.entity.Accident;
import com.iframework.mas.modules.accident.entity.Accidentlocation;
import com.iframework.mas.modules.accident.entity.Accidentresult;
import com.iframework.mas.modules.accident.service.AccidentService;

/**
 * 事故列表Controller
 * @author 余艾龙
 * @version 2016-01-30
 */
@Controller
@RequestMapping(value = "${adminPath}/accident/accident")
public class AccidentController extends BaseController {

	@Autowired
	private AccidentService accidentService;
	
	@ModelAttribute
	public Accident get(@RequestParam(required=false) String id) {
		Accident entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = accidentService.get(id);
		}
		if (entity == null){
			entity = new Accident();
		}
		return entity;
	}
	
	@RequiresPermissions("accident:accident:view")
	@RequestMapping(value = {"list", ""})
	public String list(Accident accident, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Accident> page = accidentService.findPage(new Page<Accident>(request, response), accident); 
		model.addAttribute("page", page);
		return "modules/accident/accidentList";
	}

	@RequiresPermissions("accident:accident:view")
	@RequestMapping(value = "form")
	public String form(Accident accident, Model model) {
		Accidentlocation location=accident.getAccidentlocation();
		location.setCurrentweather("19000");
		accident.setAccidentlocation(location);
		Accidentresult result=accident.getAccidentresult();
		accident.setAccidentresult(result);
		accident.setStatus("10000");
		model.addAttribute("accident", accident);
		return "modules/accident/accidentForm";
	}

	@RequiresPermissions("accident:accident:edit")
	@RequestMapping(value = "save")
	public String save(Accident accident, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, accident)){
			return form(accident, model);
		}
		accidentService.save(accident);
		addMessage(redirectAttributes, "保存事故详情成功");
		return "redirect:"+Global.getAdminPath()+"/accident/accident/?repage";
	}
	
	@RequiresPermissions("accident:accident:edit")
	@RequestMapping(value = "delete")
	public String delete(Accident accident, RedirectAttributes redirectAttributes) {
		accidentService.delete(accident);
		addMessage(redirectAttributes, "删除事故详情成功");
		return "redirect:"+Global.getAdminPath()+"/accident/accident/?repage";
	}

}